package net.ycxinxing.mcpebox.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import net.ycxinxing.mcpebox.baseActivity;
import net.ycxinxing.mcpebox.bean.mUser;
import net.ycxinxing.mcpebox.mDialog;

public class LoginActivity extends baseActivity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		LinearLayoutCompat l=new LinearLayoutCompat(this);
		l.setOrientation(1);
		TextInputLayout LUserName=new TextInputLayout(this);
		final TextInputEditText UserName=new TextInputEditText(this);
		LUserName.addView(UserName);
		LUserName.setHint("用户名");
		UserName.setMaxLines(1);
		UserName.setSingleLine(true);
		TextInputLayout LPassWord=new TextInputLayout(this);
		final TextInputEditText Password=new TextInputEditText(this);
		LPassWord.addView(Password);
		LPassWord.setHint("密码");
		Password.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
		Button login=new Button(this);
		login.setText("登陆");
		login.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					mUser user=new mUser();
					user.setUsername(UserName.getText().toString());
					user.setPassword(Password.getText().toString());
					final ProgressDialog pd=new ProgressDialog(LoginActivity.this);
					pd.setMessage("登陆中...");
					pd.setCancelable(false);
					pd.show();
					user.login(new SaveListener<mUser>(){
							@Override
							public void done(mUser p1, BmobException p2)
							{
								if(p2!=null)
								{
									mDialog dlg=new mDialog(LoginActivity.this){
										@Override
										public void onButtonClick(int i)
										{
											// TODO: Implement this method
										}
									};
									dlg.setTitle("登陆发生了错误");
									dlg.setMsg("错误码:"+p2.getErrorCode()+"\n错误详情:\n"+p2.getMessage());
									dlg.setButton3("确定");
									pd.dismiss();
									dlg.show();
								}else{
									mDialog dlg=new mDialog(LoginActivity.this){
										@Override
										public void onButtonClick(int i)
										{
											LoginActivity.this.finish();
											// TODO: Implement this method
										}
									};
									dlg.setMsg("登陆成功");
									dlg.setButton3("返回主页");
									dlg.setCanClose(false);
									pd.dismiss();
									dlg.show();
								}
								// TODO: Implement this method
							}
						});
					// TODO: Implement this method
				}
			});
		l.addView(LUserName);
		l.addView(LPassWord);
		l.addView(login);
		setContentView(l);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add("注册新账号");
		// TODO: Implement this method
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		startActivity(new Intent(this,RegisterActivity.class));
		finish();
		// TODO: Implement this method
		return super.onOptionsItemSelected(item);
	}
	
}
